package com.six.backend.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.six.backend.annotation.OperLog;
import com.six.backend.pojo.Conference;
import com.six.backend.pojo.SourceMaterial;
import com.six.backend.service.ConferenceService;
import com.six.backend.service.SourceMaterialService;
import com.six.backend.service.SummaryService;
import com.six.backend.shiro.Utils.Result;
import com.six.backend.utils.TokenUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: skyler
 * @Date: 2021/11/4 15:14
 */
@RestController
@Slf4j
@Transactional(rollbackFor=Exception.class)
@RequestMapping(value = "/conference",produces = "application/json;charset = utf-8")
public class ConferenceController {
    @Autowired
    private ConferenceService conferenceService;

    @Autowired
    private SummaryService summaryService;

    @Autowired
    private SourceMaterialService sourceMaterialService;


    @GetMapping("/getSummary")
    @ApiOperation("获取会议纪要")
    @OperLog(operModule = "会议管理模块",operType = "get",operDesc = "获取会议纪要操作")
    public Result getSummaryByConferenceId(@RequestParam(value = "pageNum",required = false,defaultValue = "1") @NotNull(message = "pageNum不能为空") Integer pageNum,
                                           @RequestParam(value = "pageSize",required = false,defaultValue = "5") @NotNull(message = "pageSize不能为空") Integer pageSize,
                                           @RequestParam(value = "summaryName",required = false) String summaryName,
                                           @RequestParam(value = "id",required = false) Integer id,
                                           HttpServletRequest request){
        String account = TokenUtil.getTokenName(request);
        Map map = new HashMap(3);
        if (summaryName != "") {
            map.put("summaryName",summaryName);
        }
        map.put("id",id);
        map.put("account",account);
        PageHelper.startPage(pageNum, pageSize);
        List list = summaryService.getSummary(map);
        return new Result<>().setCode(200).setData(new PageInfo(list)).setMessage("查询成功");
    }


    @PutMapping("/putConferenceRecord")
    @ApiOperation("修改会议信息")
    @OperLog(operModule = "会议管理模块",operType = "put",operDesc = "修改会议信息操作")
    public Result alterConference(@RequestBody Conference conference,HttpServletRequest request) {
        String account = TokenUtil.getTokenName(request);
        Integer conferenceId = conference.getId();
        if (conferenceId == null) {
            return new Result<Integer>().setCode(417).setMessage("未获取到会议编号");
        }
        if (conferenceService.isConferenceRunning(conferenceId)) {
            return new Result<>().setCode(202).setMessage("会议正在进行中，无法修改");
        }
        if (conferenceService.isConferenceHost(conferenceId,account)) {
            if (conferenceService.alterConference(conference) == 1) {
                return new Result<>().setCode(200).setMessage("修改成功");
            }
            return new Result<>().setCode(200).setMessage("信息一致，无需修改");
        }
        return new Result<>().setCode(202).setMessage("您不是主持人，无权修改");

    }

    @GetMapping("/getConference")
    @ApiOperation("模糊查询会议")
    @OperLog(operModule = "会议管理模块",operType = "get",operDesc = "模糊查询会议操作")
    public Result getConference(@RequestParam(value = "pageNum",required = false) @NotNull(message = "pageNum不能为空") Integer pageNum,
                                @RequestParam(value = "pageSize",required = false) @NotNull(message = "pageNum不能为空") Integer pageSize,
                                @RequestParam(value = "name",required = false) String name,
                                HttpServletRequest request) {
        String account = TokenUtil.getTokenName(request);
        Map map = new HashMap(1);
        if (name != "") {
            map.put("name",name);
        }
        if (pageSize != null) {
            if (pageNum != null) {
                PageHelper.startPage(pageNum, pageSize);
            }else {
                PageHelper.startPage(1, pageSize);
            }
        }
        map.put("account",account);
        List<Conference> conference = conferenceService.getConference(map);
        return new Result<>().setCode(200).setMessage("查询成功").setData(new PageInfo<>(conference));
    }

    @DeleteMapping("deleteConference")
    @ApiOperation("删除会议")
    @OperLog(operModule = "会议管理模块",operType = "delete",operDesc = "删除会议操作")
    public Result deleteConference(@RequestParam("id") @Valid Integer id,HttpServletRequest request) {
        String account = TokenUtil.getTokenName(request);
        HashMap map = new HashMap<>();
        map.put("id",id);
        Integer integer = conferenceService.deleteConference(map);
        return new Result<>().setCode(200).setMessage("成功删除"+integer+"个会议");
    }



    @PostMapping("/addConference")
    @ApiOperation("新建会议")
    @OperLog(operModule = "会议管理模块",operType = "post",operDesc = "新建会议操作")
    public Result addConference(@RequestBody Conference conference) {
        if (conferenceService.addConference(conference)) {
            return new Result<>().setCode(201).setMessage("新建会议成功");
        }
        return new Result<>().setCode(202).setMessage("新建会议失败");
    }

    @GetMapping("getConferenceInformation")
    @ApiOperation("获取开始会议详细信息")
    @OperLog(operModule = "会议管理模块",operType = "get",operDesc = "开始会议操作")
    public Result getConferenceInformation(@RequestParam Integer id) {
        Conference conference = conferenceService.getConferenceInformation(id);

        return new Result<>().setCode(200).setMessage("操作成功").setData(conference);
    }

    @GetMapping("getConferenceProcess")
    @ApiOperation("获取会议过程记录")
    @OperLog(operModule = "会议管理模块",operType = "get",operDesc = "获取会议过程记录操作")
    public Result getConferenceProcess(@RequestParam("id") @NotNull(message = "会议id不能为空") Integer id) {
        Conference conference = conferenceService.getConferenceInformation(id);

        return new Result<>().setCode(200).setMessage("查询会议过程记录成功").setData(conference);
    }

    @GetMapping("getConferenceSource")
    @ApiOperation("获取会议素材信息")
    @OperLog(operModule = "会议管理模块",operType = "get",operDesc = "获取会议素材")
    public Result getConferenceSource(@RequestParam("id") @NotNull Integer id,
                                      @RequestParam("pageSize") @NotNull Integer pageSize,
                                      @RequestParam("pageNum") @NotNull Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<SourceMaterial> list = conferenceService.selectConferenceSourceById(id);
        return new Result<>().setCode(200).setMessage("查询会议素材成功").setData(new PageInfo<>(list));
    }

    @GetMapping("getConferenceSourceByName")
    @ApiOperation("模糊查询会议素材")
    @OperLog(operModule = "会议管理模块",operType = "get",operDesc = "模糊查询会议素材")
    public Result getConferenceSourceByName(@RequestParam("id") @NotNull Integer id,
                                            @RequestParam(value = "name", required = false) @NotNull String name,
                                            @RequestParam("pageSize") @NotNull Integer pageSize,
                                            @RequestParam("pageNum") @NotNull Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<SourceMaterial> list = conferenceService.selectConferenceSourceByName(id,name);
        return new Result<>().setCode(200).setMessage("查询会议素材成功").setData(list);
    }

    @GetMapping("getSummaryWriterConference")
    @ApiOperation("获取用户能上传会议纪要的会议信息")
    @OperLog(operModule = "会议管理模块",operType = "get",operDesc = "获取用户能上传会议纪要的会议信息")
    public Result selectSummaryWriterConference(HttpServletRequest request) {
        String account = TokenUtil.getTokenName(request);
        List<Conference> conference = summaryService.getConference(account);
        return new Result<>().setCode(200).setMessage("查询会议信息成功").setData(conference);
    }

    @PutMapping("startConference")
    @ApiOperation("开始会议")
    public Result startConference(@RequestBody Conference conference,HttpServletRequest servletRequest) {
        String account = TokenUtil.getTokenName(servletRequest);
        if (account == null) {
            return new Result().setCode(401).setMessage("请先登录");
        }
        if (conferenceService.isConferenceHost(conference.getId(),account)) {
            if (conferenceService.startConference(conference.getId())) {
                return new Result().setCode(200).setMessage("开启会议成功");
            }
            return new Result().setCode(202).setMessage("开启会议失败");
        }
        return new Result().setCode(202).setMessage("您不是主持人，无权限开始会议");
    }

    @PutMapping("/endConference")
    @ApiOperation("结束会议")
    public Result endConference(@RequestBody Conference conference,HttpServletRequest servletRequest) {
        String account = TokenUtil.getTokenName(servletRequest);
        if (account == null) {
            return new Result().setCode(401).setMessage("请先登录");
        }
        if (conferenceService.isConferenceHost(conference.getId(),account)) {
            if (conferenceService.endConference(conference.getId())) {
                return new Result().setCode(200).setMessage("结束会议成功");
            }
            return new Result().setCode(202).setMessage("还有议题未结束，无法结束会议");
        }
        return new Result().setCode(202).setMessage("您不是主持人，无权限结束会议");
    }

    @DeleteMapping("/deleteSummary")
    @ApiOperation("删除会议纪要")
    public Result deleteSummary(@RequestParam("summaryId") Integer summaryId) {
        if (summaryService.deleteSummary(summaryId)) {
            return new Result().setCode(200).setMessage("会议纪要删除成功");
        }
        return new Result().setCode(202).setMessage("会议纪要删除失败");
    }
}
